﻿using System;
using System.Collections.Generic;
//using System.Threading.Tasks;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace GetContent
{
    public partial class Form1 : Form
    {
        private static int numOfLink ;
        public Form1()
        {
            InitializeComponent();

            PhoneList.Initialize();//thieets lap
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lbStatus.Text = "Ready";
            //dtFrom.CustomFormat = "dd-MM-yyyy";
            //dtTo.CustomFormat = "dd-MM-yyyy";
            
        }

        private  void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            SaveConfigs();
          
            var rv = new DataFilterRaoVat();
            var link = txtLink.Text;
            if (link == "")
            {
                MessageBox.Show("Empty link", "Warning");
                return;
            }
           
            if (link.Contains("raovat.com") == false)
            {
                MessageBox.Show("Only support Raovat.com");
                return;
            }
            var listLink = rv.getLinks(link);



            if (listLink == null)
            {
                MessageBox.Show("Can not get Links", "Warning");
            }
            else
            {
                //Hung
                dgvLinks.Rows.Clear();
                //Hung

                
                lbTotal.Text = listLink.Count.ToString();
               
                

                foreach (var l in listLink)
                {
                    dgvLinks.Rows.Add(l, @"http://raovat.com", "Ready!");  
                }



                //var mainThread = new Thread(Start);
                //mainThread.Start();

                //Thread.Sleep(10000);
                Start();



            }
            

            
            
        }

        void Start()
        {
            
            
            foreach (DataGridViewRow row in dgvLinks.Rows)
            {
                DataFilterRaoVat fClass = new DataFilterRaoVat();
                fClass.Status = dgvLinks["stt", row.Index];
                fClass.Start(row.Cells["link"].Value.ToString(), txtfilter.Text);
                //break;
                

            }

            numOfLink = dgvLinks.RowCount;

           

          
            
        }



      


        private void SaveConfigs()
        {
            Configs.WriteValue("phoneLimit", txtPhoneLimit.Text);
            Configs.WriteValue("fromDate", dtFrom.Text);
            Configs.WriteValue("toDate", dtTo.Text);
        }

        private void btnRestart_Click(object sender, EventArgs e)
        {

            
            Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            Process.GetCurrentProcess().Kill();
            

        }

        private void dgvLinks_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var rowIndex = e.RowIndex;
            var row = dgvLinks.Rows[rowIndex];
            Process.Start(row.Cells[0].Value.ToString());
            
            //MessageBox.Show(row.Cells[0].Value.ToString());

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }

        private void dtFrom_ValueChanged(object sender, EventArgs e)
        {
            if (dtFrom.Value > dtTo.Value)
            {
                MessageBox.Show("From Date must less than To Date");
                return;
            }
        }

        private void dtTo_ValueChanged(object sender, EventArgs e)
        {
            if (dtTo.Value < dtFrom.Value )
            {
                MessageBox.Show("From Date must greater than To Date");
                return;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int check = 0;
            List<int> listCompleted = new List<int>();
            foreach (DataGridViewRow row in dgvLinks.Rows)
            {

                string[] rs = new string[] { "Cannot Parse HTML", "Fail!Not Match Condition", "Completed!", "Cannot get HTML" };
                var value = row.Cells["stt"].Value;
                if (value == "Completed!") {listCompleted.Add(row.Index);}
                foreach (object o in rs)
                {
                    if (Equals(o, value))
                    {
                        check ++;
                        break;
                    }
                }
                //break;
            }
            if (check == numOfLink)
            {
                timer1.Stop();
                foreach (DataGridViewRow row in dgvLinks.Rows)
                {
                    if (!listCompleted.Contains(row.Index))
                    {
                        dgvLinks.Rows[row.Index].Visible = false;
                    }
                }
                MessageBox.Show("Completed","Message");
                //timer1.Enabled = false;
               
            }
            
        }

       


       
    }
}
